define(['city', 'form', 'ui', 'date'], function(require, exports, module) {
    function bindUploadPhoto() {
        require.async('photo', function(a) {
            a.init({
                clickObj: $('.js-show-up')
            });
        });
    }

    function processPhoto() {
        $('.dog-photo .thumbnail img').each(function() {
            var obj = $(this);
            var src = obj.attr('data-src');
            if (!src) {
                return;
            }
            var img = new Image();
            img.onload = function() {
                obj.attr('src', src);
                var w = obj.width();
                var h = obj.height();
                if (w >= h) {
                    obj.css({'height': '100%'});
                } else {
                    obj.css({'width': '100%'});
                }
            }
            img.src = src;
        });
    }

    exports.init = function() {
        bindUploadPhoto();
        processPhoto();
        $('#city').city({
            code: $('input[name="city"]').val(),
            callback: function(code, name) {
                $('#js-city-input').val(code);
            }
        });
        $('.js-show-hide-edit').on('click', function() {
            var well = $(this).closest('.well');
            var desc = well.find('.desc');
            var one = desc.eq(0);
            var two = desc.eq(1);
            if (one.is(':visible')) {
                one.slideUp();
                two.slideDown();
                $(this).html('<i class="glyphicon glyphicon-edit"></i> 取消');
            } else {
                two.slideUp();
                one.slideDown();
                $(this).html('<i class="glyphicon glyphicon-edit"></i> 编辑');
            }
        });
        $('#js-condition').on('submit', function() {
            $(this).ajaxSubmit({
                url: '/Ajax/User/saveCondition?stmp=' + new Date().toString(),
                type: 'post',
                dataType: 'json',
                success: function(d) {
                    if (!d.error) {
                        window.location.reload();
                    } else {
                    }
                }
            });
            return false;
        });
        $('#js-user-master-edit-form').on('submit', function() {
            $(this).ajaxSubmit({
                url: '/Ajax/User/saveMaster?stmp=' + new Date().toString(),
                type: 'post',
                dataType: 'json',
                success: function(d) {
                    if (!d.error) {
                        window.location.reload();
                    } else {
                        $('#js-user-master-edit-form').find('.alert').show().find('.msg').html(d.data);
                    }
                }
            });
            return false;
        });
        $('.js-article-del').on('click', function() {
            if (!confirm('你确定要删除吗？')) {
                return;
            }
            var articleId = $(this).attr('articleId');
            $.ajax({
                url: '/Ajax/Article/del',
                type: 'post',
                data: {articleId: articleId},
                dataType: 'json',
                success: function(d) {
                    if (d.error) {
                        alert(d.data);
                    } else {
                        window.location.reload();
                    }
                }
            });
        });
        require.async('breed', function(a) {
            a.muInit($('#js-input-breed'));
        });
        $("#birthday").DateSelector({
            ctlYearId: 'birthday-y',
            ctlMonthId: 'birthday-m',
            ctlDayId: 'birthday-d',
            showNull: true,
            minYear: new Date().getFullYear() - 70,
            maxYear: new Date().getFullYear()
        });

    }
});